package com.yisu.expressway.onedollar.activity;

import com.yisu.expressway.R;
import com.yisu.expressway.onedollar.adapter.MyFragmentPagerAdapter;
import com.yisu.expressway.onedollar.fragment.AllOrderFragment;
import com.yisu.expressway.onedollar.fragment.OrderShowFragment;
import com.yisu.expressway.onedollar.fragment.WinningOrderFragment;
import com.yisu.expressway.onedollar.view.IMyOrderView;
import com.yisu.expressway.onedollar.widget.TitleView;
import com.yisu.expressway.utils.DisplayUtil;
import com.yisu.expressway.utils.HBLog;
import com.yisu.expressway.utils.StringUtil;
import com.yisu.expressway.utils.ToastUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by shijunfeng on 2016/12/18.
 */
public class OneDollarRecordActivity extends BasicActivity implements IMyOrderView {
    private static final String TAG = OneDollarRecordActivity.class.getSimpleName();
    @Bind(R.id.title_bar)
    protected TitleView mTitleView;
    @Bind(R.id.viewPager)
    protected ViewPager mViewPager;
    @Bind(R.id.tab_layout)
    protected TabLayout mTabLayout;
    private MyFragmentPagerAdapter mFragAdapter;

    @Override
    protected int getThemeId() {
        return R.style.OneDollarTheme;
    }

    @Override
    protected int getInflateLayoutId() {
        return R.layout.activity_onedollar_my_order;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void releasePresenter() {

    }

    @Override
    protected void getIntentData(Intent intent) {

    }

    @Override
    protected void getSavedBundleData(Bundle bundle) {

    }
	private AllOrderFragment allOrderFragment;
	private WinningOrderFragment winningOrderFragment;
	private OrderShowFragment orderShowFragment;
    @Override
    protected void initViews() {
        List<Fragment> fragments = new ArrayList<>();
	    if (allOrderFragment == null){
		    allOrderFragment = new AllOrderFragment();
	    }

	    if (winningOrderFragment == null){
		    winningOrderFragment = new WinningOrderFragment();
	    }

	    if (orderShowFragment == null){
		    orderShowFragment = new OrderShowFragment();
	    }
        fragments.add(allOrderFragment);
        fragments.add(winningOrderFragment);
        fragments.add(orderShowFragment);
	    List<String> titleList = new ArrayList<>();
	    titleList.add("抢宝记录");
	    titleList.add("抢中记录");
	    titleList.add("晒单记录");
        mFragAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), titleList, fragments);
        mViewPager.setAdapter(mFragAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
	    setIndicatorWidth(this,mTabLayout,36,36);
    }
	private void setIndicatorWidth(Context context, TabLayout tabs, int leftDip, int rightDip) {
		Class<?> tabLayout = tabs.getClass();
		Field tabStrip = null;
		try {
			tabStrip = tabLayout.getDeclaredField("mTabStrip");
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}

		tabStrip.setAccessible(true);
		LinearLayout ll_tab = null;
		try {
			ll_tab = (LinearLayout) tabStrip.get(tabs);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		int left = DisplayUtil.dip2px(context,leftDip);
		int right = DisplayUtil.dip2px(context,rightDip);

		for (int i = 0; i < ll_tab.getChildCount(); i++) {
			View child = ll_tab.getChildAt(i);
			child.setPadding(0, 0, 0, 0);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
			params.leftMargin = left;
			params.rightMargin = right;
			child.setLayoutParams(params);
			child.invalidate();
		}
	}
    @Override
    protected void bindEvents() {
        mTitleView.setOnTitleBarClickEvent(new TitleView.OnTitleBarClickEvent() {
            @Override
            public void onLeftClick(View view) {
                finish();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
    }

    @Override
    protected boolean isNeedLogin() {
        return true;
    }

    @Override
    protected void onLoginStateChange(boolean login) {
        HBLog.i(TAG,"<onLoginStateChange>  "+login);
	    if (login){
		    initViews();
	    }else {
		    finish();
	    }

    }

    @Override
    public void showToast(String toastMsg) {
        if (StringUtil.isNullOrEmpty(toastMsg)) {
            return;
        }
        ToastUtil.displayLongToastMsg(this, toastMsg);
    }

    @Override
    public void showLoadingDlg() {
        showLoading();
    }

    @Override
    public void dismissLoadingDlg() {
        dismissLoading();
    }

    @Override
    public void jump2OrderDetailPage() {
        //TODO
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (allOrderFragment != null){
			allOrderFragment.onActivityResult(requestCode, resultCode, data);
		}
		if (winningOrderFragment != null){
			winningOrderFragment.onActivityResult(requestCode, resultCode, data);
		}
		if (orderShowFragment != null){
			orderShowFragment.onActivityResult(requestCode, resultCode, data);
		}
	}
}
